package controllayer;

import java.sql.SQLException;
import java.util.ArrayList;

import modellayer.Customer;
import modellayer.PartOrder;
import modellayer.Product;
import modellayer.SalesOrder;
import dblayer.*;

public class SalesOrderCtr {
	private IFDBSalesOrder dbSO;
	private IFDBPartOrder dbPO;

	public SalesOrderCtr() {
		dbSO = new DBSalesOrder();
		dbPO = new DBPartOrder();
	}
	
	public SalesOrder findSalesOrder(int salesOrderID) throws SQLException {
		return dbSO.selectSingleSalesOrder(salesOrderID, true);
	}

	/**
     * Adds all SalesOrder objects to a two dimensional array so they can be used in a JTable in ViewOrderPanel
     * @return array data[][]
     */
    public String[][] salesOrderToTableArray()
    {
       ArrayList<SalesOrder> orderList = dbSO.getAllSalesOrders(true);
    	
       int index = 0; 
       String data[][] = new String[orderList.size()][5];
       while(index < orderList.size())
       {
    	   SalesOrder salesOrder = orderList.get(index);
    	   
    	   data[index][0] = Integer.toString(salesOrder.getSalesOrderID());
           data[index][1] = salesOrder.getDate();
           data[index][2] = salesOrder.getDeliveryStatus();
           data[index][3] = salesOrder.getDeliveryDate();
           if(salesOrder.getCustomer() != null) {
        	   data[index][4] = salesOrder.getCustomer().getName();
           } else {
        	   data[index][4] = "";
           }
           index++;
       }

       return data;
    }
    
    public String[][] partOrderToTableArray(int salesOrderID)
    {
       ArrayList<PartOrder> partOrderList =  dbPO.getAllPartOrdersByOrder(salesOrderID, true); 
    	
       int index = 0; 
       String data[][] = new String[partOrderList.size()][3];
       while(index < partOrderList.size())
       {
    	   PartOrder partOrder = partOrderList.get(index);
    	   
    	   data[index][0] = partOrder.getProduct().getName();
    	   data[index][1] = Integer.toString(partOrder.getAmount());
    	   data[index][2] = Integer.toString(partOrder.getAmount()*partOrder.getProduct().getSalesPrice());
    	   
           index++;
       }

       return data;

    }

    
    public String[][] partOrderToTableArrayNewOrder(SalesOrder sO)
    {
       ArrayList<PartOrder> partOrderList =  sO.getPartOrders();

       int index = 0; 
       String data[][] = new String[partOrderList.size()][3];
       while(index < partOrderList.size())
       {
    	   PartOrder partOrder = partOrderList.get(index);
    	   
    	   data[index][0] = partOrder.getProduct().getName();
    	   data[index][1] = Integer.toString(partOrder.getAmount());
    	   data[index][2] = Integer.toString(partOrder.getAmount()*partOrder.getProduct().getSalesPrice());
    	   
           index++;
       }


       return data;

    }

    public void addPartOrder(int amount, Product p, int salesOrderID, SalesOrder sO) throws SQLException
    {
    	PartOrder pO = new PartOrder();
    	pO.setAmount(amount);
    	pO.setSalesOrderID(dbSO.getMaxID()+1);
    	
    	pO.setProduct(p);
    	pO.setProductID(p.getProductID());
    	sO.addPartOrder(pO);
    	
    }
    
    public void createSalesOrder(SalesOrder sO) throws SQLException {
    	dbSO.insertSalesOrder(sO);
    }
    
    public void createPartOrder(PartOrder pO) throws SQLException {
    	dbPO.insertPartOrder(pO);
    }

}
